package com.chucang.shucang.common.mq.config;

import com.chucang.shucang.common.mq.constant.RabbitConstant;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.annotation.Bean;

import java.util.HashMap;
import java.util.Map;


/**
 * @author flitsneak
 * @email flitsneak@gmail.com
 * @date 2022/10/5 17:23
 * @description
 */
@Slf4j
@RequiredArgsConstructor
@AutoConfiguration
public class RabbitConfig {

    /**
     * json 序列化
     *
     * @return m
     */
    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    /**
     * 订单服务交换机
     *
     * @return e
     */
    @Bean
    public Exchange orderEventExchange() {
        return new TopicExchange(RabbitConstant.ORDER_EXCHANGE, true, false);
    }

    /**
     * 抢购队列
     *
     * @return q
     */
    @Bean
    public Queue orderPanicPurchaseOrderQueue() {
        return new Queue(RabbitConstant.ORDER_PANIC_PURCHASE_QUEUE, true, false, false);
    }

    /**
     * 抢购绑定
     *
     * @return b
     */
    @Bean
    public Binding orderPanicPurchaseOrderQueueBinding() {
        return new Binding(
                RabbitConstant.ORDER_PANIC_PURCHASE_QUEUE,
                Binding.DestinationType.QUEUE,
                RabbitConstant.ORDER_EXCHANGE,
                RabbitConstant.PANIC_PURCHASE_KEY,
                null);
    }

    /**
     * 抢购订单死信队列
     *
     * @return q
     */
    @Bean
    public Queue orderDelayQueue() {
        Map<String, Object> arguments = new HashMap<>(4);
        arguments.put(RabbitConstant.DEAD_LETTER_EXCHANGE, RabbitConstant.ORDER_EXCHANGE);
        arguments.put(RabbitConstant.DEAD_LETTER_KEY, RabbitConstant.ORDER_PANIC_PURCHASE_RELEASE_KEY);
        //六分钟超时时间
        arguments.put(RabbitConstant.DEAD_LETTER_TTL, 360000);
        return new Queue(RabbitConstant.ORDER_PANIC_PURCHASE_DELAY_QUEUE, true, false, false, arguments);
    }

    /**
     * 抢购死信队列绑定
     *
     * @return b
     */
    @Bean
    public Binding orderPanicPurchaseOrderDelayBinding() {
        return new Binding(RabbitConstant.ORDER_PANIC_PURCHASE_DELAY_QUEUE, Binding.DestinationType.QUEUE, RabbitConstant.ORDER_EXCHANGE, RabbitConstant.PANIC_PURCHASE_DELAY_KEY, null);
    }

    /**
     * 订单释放队列
     *
     * @return q
     */
    @Bean
    public Queue orderPanicPurchaseReleaseQueue() {
        return new Queue(RabbitConstant.ORDER_PANIC_PURCHASE_RELEASE_QUEUE, true, false, false);
    }

    /**
     * 订单释放队列绑定
     *
     * @return b
     */
    @Bean
    public Binding orderPanicPurchaseReleaseBinding() {
        return new Binding(RabbitConstant.ORDER_PANIC_PURCHASE_RELEASE_QUEUE, Binding.DestinationType.QUEUE, RabbitConstant.ORDER_EXCHANGE, RabbitConstant.ORDER_PANIC_PURCHASE_RELEASE_KEY, null);
    }

    /**
     * 下单延迟队列
     *
     * @return q
     */
    @Bean
    public Queue orderPanicPurchaseDelayOrderQueue() {
        Map<String, Object> arguments = new HashMap<>(4);
        arguments.put(RabbitConstant.DEAD_LETTER_EXCHANGE, RabbitConstant.ORDER_EXCHANGE);
        arguments.put(RabbitConstant.DEAD_LETTER_KEY, RabbitConstant.ORDER_CREATE_ORDER_KEY);
        //六分钟超时时间
        arguments.put(RabbitConstant.DEAD_LETTER_TTL, 360000);
        return new Queue(RabbitConstant.ORDER_DELAY_QUEUE, true, false, false, arguments);
    }

    /**
     * 购买死信队列绑定
     *
     * @return b
     */
    @Bean
    public Binding orderCreateOrderDelayBinding() {
        return new Binding(RabbitConstant.ORDER_DELAY_QUEUE, Binding.DestinationType.QUEUE, RabbitConstant.ORDER_EXCHANGE, RabbitConstant.ORDER_CREATE_ORDER_KEY, null);
    }

    /**
     * 订单释放队列
     *
     * @return q
     */
    @Bean
    public Queue orderReleaseQueue() {
        return new Queue(RabbitConstant.ORDER_RELEASE_ORDER_QUEUE, true, false, false);
    }

    /**
     * 订单释放队列绑定
     *
     * @return b
     */
    @Bean
    public Binding orderReleaseBinding() {
        return new Binding(RabbitConstant.ORDER_RELEASE_ORDER_QUEUE, Binding.DestinationType.QUEUE, RabbitConstant.ORDER_EXCHANGE, RabbitConstant.ORDER_RELEASE_ORDER_KEY, null);
    }

    /**
     * nft交换机
     *
     * @return e
     */
    @Bean
    public Exchange nftEventExchange() {
        return new TopicExchange(RabbitConstant.NFT_EXCHANGE, true, false);
    }

    /**
     * nft销毁队列
     *
     * @return q
     */
    @Bean
    public Queue ntfDestroyQueue() {
        return new Queue(RabbitConstant.NFT_DESTROY_NFT_QUEUE, true, false, false);
    }

    /**
     * nft销毁绑定
     *
     * @return
     */
    @Bean
    public Binding ntfDestroyBinding() {
        return new Binding(RabbitConstant.NFT_DESTROY_NFT_QUEUE, Binding.DestinationType.QUEUE, RabbitConstant.NFT_EXCHANGE, RabbitConstant.NFT_DESTROY_NFT_KEY, null);
    }

    /**
     * nft发布队列
     *
     * @return q
     */
    @Bean
    public Queue ntfPublishQueue() {
        return new Queue(RabbitConstant.NFT_PUBLISH_NFT_QUEUE, true, false, false);
    }

    /**
     * nft发布绑定
     *
     * @return b
     */
    @Bean
    public Binding ntfPublishBinding() {
        return new Binding(RabbitConstant.NFT_PUBLISH_NFT_QUEUE, Binding.DestinationType.QUEUE, RabbitConstant.NFT_EXCHANGE, RabbitConstant.NFT_PUBLISH_NFT_KEY, null);
    }

    /**
     * pay交换机
     *
     * @return e
     */
    @Bean
    public Exchange payEventExchange() {
        return new TopicExchange(RabbitConstant.PAY_EXCHANGE, true, false);
    }

    /**
     * 提现队列
     *
     * @return q
     */
    @Bean
    public Queue aliPayWithdrawQueue() {
        return new Queue(RabbitConstant.ALIPAY_WITHDRAW_QUEUE, true, false, false);
    }

    /**
     * 提现绑定
     *
     * @return b
     */
    @Bean
    public Binding aliPayWithdrawBinding() {
        return new Binding(RabbitConstant.ALIPAY_WITHDRAW_QUEUE, Binding.DestinationType.QUEUE, RabbitConstant.PAY_EXCHANGE, RabbitConstant.ALIPAY_WITHDRAW_KEY, null);
    }

    /**
     * 盲盒支付成功队列
     *
     * @return
     */
    @Bean
    public Queue aliPayBlindBoxSuccessQueue() {
        return new Queue(RabbitConstant.ALIPAY_BLIND_BOX_SUCCESS_QUEUE, true, false, false);
    }

    /**
     * 盲盒支付成功绑定
     *
     * @return
     */
    @Bean
    public Binding aliPayBlindBoxSuccessBinding() {
        return new Binding(RabbitConstant.ALIPAY_BLIND_BOX_SUCCESS_QUEUE, Binding.DestinationType.QUEUE, RabbitConstant.PAY_EXCHANGE, RabbitConstant.ALIPAY_BLIND_BOX_SUCCESS_KEY, null);
    }

    /**
     * 藏品支付成功队列
     *
     * @return
     */
    @Bean
    public Queue aliPayCollectionSuccessQueue() {
        return new Queue(RabbitConstant.ALIPAY_COLLECTION_SUCCESS_QUEUE, true, false, false);
    }

    /**
     * 藏品支付成功绑定
     *
     * @return
     */
    @Bean
    public Binding aliPayCollectionSuccessBinding() {
        return new Binding(RabbitConstant.ALIPAY_COLLECTION_SUCCESS_QUEUE, Binding.DestinationType.QUEUE, RabbitConstant.PAY_EXCHANGE, RabbitConstant.ALIPAY_COLLECTION_SUCCESS_KEY, null);
    }

    /**
     * 阿里充值成功队列
     *
     * @return
     */
    @Bean
    public Queue aliPayRechargeSuccessQueue() {
        return new Queue(RabbitConstant.ALIPAY_RECHARGE_SUCCESS_QUEUE, true, false, false);
    }

    /**
     * 充值成功绑定
     *
     * @return
     */
    @Bean
    public Binding aliPayRechargeSuccessBinding() {
        return new Binding(RabbitConstant.ALIPAY_RECHARGE_SUCCESS_QUEUE, Binding.DestinationType.QUEUE, RabbitConstant.PAY_EXCHANGE, RabbitConstant.ALIPAY_RECHARGE_SUCCESS_KEY, null);
    }

    /**
     * 关单队列
     *
     * @return
     */
    @Bean
    public Queue aliPayCloseOrderQueue() {
        return new Queue(RabbitConstant.ALIPAY_CLOSE_ORDER_QUEUE, true, false, false);
    }

    /**
     * 关单绑定
     *
     * @return
     */
    @Bean
    public Binding aliPayCloseOrderBinding() {
        return new Binding(RabbitConstant.ALIPAY_CLOSE_ORDER_QUEUE, Binding.DestinationType.QUEUE, RabbitConstant.PAY_EXCHANGE, RabbitConstant.ALIPAY_CLOSE_ORDER_KEY, null);
    }
}
